#ifndef __APP_MAIN_H__
#define __APP_MAIN_H__

//#include "ipcc.h"
//#include "openamp.h"

#include "cmsis_os2.h"
#include "bms_can.h"
#include "power_can.h"
#include "utils.h"

#ifdef __cplusplus
extern "C" {
#endif

#define CHARGE_M_CORE_VERSION   0x0002          /* 固件版本号 */

#define OVER_TEMP_GATE          85
#define TARGET_ERROR            10000           /* 设置电源模块的设置值和实际值的允许误差 */
#define PRECHARGE_OFFSET        10000           /* 预充电的偏移电压 */

/**
 * @brief 主状态机
 */
#define CHARGE_IDLE_STATE       (1 << 0u)
#define CHARGE_START_STATE      (1 << 1u)       
#define CHARGE_SHAKE_STATE      (1 << 2u)
#define CHARGE_IDENTIFY_STATE   (1 << 3u)
#define CHARGE_CONFIG_STATE     (1 << 4u)
#define CHARGE_KEEP_STATE       (1 << 5u)
#define CHARGE_END_STATE        (1 << 6u)
#define CHARGE_INSU_STATE       (1 << 7u)

/**
 * @brief 子状态机 握手阶段
 */
#define SHAKE_IDLE_STATE        (1 << 0u)
#define SHAKE_START_STATE       (1 << 1u)
#define SHAKE_END_STATE         (1 << 2u)

/**
 * @brief 子状态机 辨识阶段
 */
#define IDENTIFY_IDLE_STATE     (1 << 0u)
#define IDENTIFY_START_STATE    (1 << 1U)
#define IDENTIFY_ACK_STATE      (1 << 2u)
#define IDENTIFY_END_STATE      (1 << 3u)

/**
 * @brief 子状态机 参数配置阶段
 */
#define CONFIG_IDLE_STATE       (1 << 0u)
#define CONFIG_TSS_STATE        (1 << 1u)       /* 时间同步信息发送 */
#define CONFIG_CRO00_STATE      (1 << 2u)
#define CONFIG_CROAA_STATE      (1 << 3u)
#define CONFIG_END_STATE        (1 << 4u)

/**
 * @brief 子状态机 充电过程阶段
 */
#define CHARGING_IDLE_STATE     (1 << 0u)
#define CHARGING_BCL_STATE      (1 << 1u)
#define CHARGING_BCS_STATE      (1 << 2u)
#define CHARGING_JUDGE_STATE    (1 << 3u)
#define CHARGING_END_STATE      (1 << 4u)
#define CHARGING_SUSPEND_STATE  (1 << 5u)
/**
 * @brief 子状态机 充电结束阶段
 */
#define ENDING_IDLE_STATE_STATE       (1 << 0u)
#define ENDING_SEND_CEM_STATE         (1 << 1u)
#define ENDING_SEND_CST_STATE         (1 << 2u)
#define ENDING_CLOSE_PWR_STATE        (1 << 3u)
#define ENDING_RP_STATE               (1 << 4u)
#define ENDING_OPEN_ELE_LOCK_STATE    (1 << 5u)
#define ENDING_SEND_CSD_STATE         (1 << 6u)

/**
 * @brief 状态机结果
 */
#define FSM_SUCCESS             0
#define FSM_FAIL                1
#define FSM_RUN                 2

/**
 * @brief 充电状态
 */
#define CHARGEING               (1 << 0u)       /* 充电中 */
#define CHARGE_END              (1 << 1u)       /* 充电结束 */
#define CHARGE_FAULT            (1 << 2u)       /* 充电故障 */

/**
 * @brief 充电故障
 */
#define CHARGE_BMS_FAULT        (1 << 0u)       /* 充电中BMS故障 */
#define CHARGE_K1K2_FAULT       (1 << 1u)       /* K1K2继电器粘连故障 */
#define CAHAGE_CC1_FAULT        (1 << 2u)       /* CC1连接故障 */
#define CHARGE_IDF_FAULT        (1 << 3u)       /* 绝缘检测故障 */
#define CHARGE_PE_FAULT         (1 << 4u)       /* 接地故障 */

/**
 * @brief 充电报警
 */
#define CHARGE_PE_ALARM         (1 << 0u)

typedef struct
{
    uint32_t start_timestamp;
    uint32_t time;
} charge_time_t;

typedef struct
{
    int status;
    bms_t bms;
    pwr_t pwr;
    charge_time_t charge_time;  /* 充电统计 */
    void *userdata;
} app_t;

extern osThreadId_t appTaskHandle;
extern const osThreadAttr_t appTask_attributes;
void apptask(void *argument);

#ifdef __cplusplus
}
#endif

#endif  /* __APP_MAIN_H__ */


